package com.mt.api.modules.service.impl;


import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.api.constants.enums.OrderStatusEnum;
import com.mt.api.constants.enums.OutStoreStatusEnum;
import com.mt.api.modules.domain.AppStock;
import com.mt.api.modules.domain.OutStore;
import com.mt.api.modules.domain.OutStoreDetail;
import com.mt.api.modules.domain.bo.OutStoreCancelBo;
import com.mt.api.modules.manager.*;
import com.mt.api.modules.service.IOutStoreService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author: plover
 * @Date: 2022-05-30
 * @Description:
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OutStoreServiceImpl implements IOutStoreService {

    private final IOutStoreManager outStoreManager;
    private final IOutStoreDetailManager outStoreDetailManager;
    private final IOrderDetailManager orderDetailManager;
    private final IStockManager stockManager;
    private final IOrderManager orderManager;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WebResponse cancelOutStore(OutStoreCancelBo bo) {
        OutStore outStore = outStoreManager.getById(bo.getOutStoreId());
        if (Objects.isNull(outStore)) {
            return WebResponseUtils.error("出库单不存在");
        }
        if (Objects.equals(outStore.getStatus(), OutStoreStatusEnum.CANCEL.getCode())) {
            return WebResponseUtils.error("出库单已撤销，不可重复撤销");
        }
        OutStore upOutStore = new OutStore();
        upOutStore.setId(outStore.getId());
        upOutStore.setStatus(OutStoreStatusEnum.CANCEL.getCode());
        outStoreManager.updateById(upOutStore);

        List<OutStoreDetail> outStoreDetailList = outStoreDetailManager.getByOutStoreId(bo.getOutStoreId());
        /**
         * 1、修改库存数量
         * 2、修改对应的销售单出库数量
         */
        for (OutStoreDetail outStoreDetail : outStoreDetailList) {
            Integer orderDetailId = outStoreDetail.getOrderDetailId();
            orderDetailManager.subtractOutStoreQty(orderDetailId, outStoreDetail.getQty());

            AppStock stock = stockManager.getStockBySkuId(outStoreDetail.getSkuId());
            if (Objects.nonNull(stock)) {
                stockManager.plusQtyBySkuId(outStoreDetail.getSkuId(), outStoreDetail.getQty());
            }
        }

        /**
         * 撤销出库数量
         * 判断销售单出库状态是否需要更新
         * 如果没有出库单，则更新状态
         */
        List<OutStore> outStoreList = outStoreManager.getListByOrderId(outStore.getOrderId());
        if (CollectionUtils.isEmpty(outStoreList)) {
            orderManager.subtractOutStoreQty(outStore.getOrderId(), outStore.getQty(), OrderStatusEnum.UN_OUT_STORE.getCode());
        } else {
            orderManager.subtractOutStoreQty(outStore.getOrderId(), outStore.getQty(), OrderStatusEnum.PART_OUT_STORE.getCode());
        }
        return WebResponseUtils.success();
    }
}
